package com.yusong.open.sdk;

import com.google.gson.Gson;
import com.yusong.open.sdk.domains.*;
import com.yusong.open.sdk.utils.OkHttpUtil;
import com.yusong.open.sdk.utils.SerialNumberUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;

/**
 * Created by yusong on 2018/8/5.
 * 接入二维码验证
 */
public class QrService {

    private final Logger logger = LoggerFactory.getLogger(QrService.class);

    //生成二维码服务地址
    private static final String GENERATE_QR_URL = "http://www.goubula.com/qr/generate";

    //查询二维码
    private static final String QUERY_QR_URL = "http://www.goubula.com/qr/query";


    private AccountInfo accountInfo;

    public QrService(AccountInfo accountInfo) {
        this.accountInfo = accountInfo;
    }

    /**
     * 获取认证二维码
     * @return
     */
    public GenerateQrRes getCode() throws OpenSDKException{
        GenerateQrReq req = new GenerateQrReq();
        req.setBusinessId(accountInfo.getBusinessId());
        req.setBusinessTradeNo(SerialNumberUtil.generateSerialNumber());
        String jsonRes = null;
        Gson gson = new Gson();
        String reqJson = gson.toJson(req);
        logger.info("getCode|req:{}",reqJson);
        try {
            jsonRes = OkHttpUtil.postJson(GENERATE_QR_URL,reqJson);
        } catch (Exception e) {
            logger.error("getCode|error|",e);
            return null;
        }
        logger.info("getCode|res:{}",jsonRes);
        Type type = new ParameterizeTypeImpl(BaseRes.class, new Class[]{GenerateQrRes.class});

        BaseRes<GenerateQrRes> res = gson.fromJson(jsonRes,type);
        if(0 != res.getCode()){
            logger.error("getCode|get qr code error|{}",res);
            throw new OpenSDKException(res.getCode(),res.getMsg());
        }
        GenerateQrRes data = res.getData();
        logger.info("getCode|res:{}",data);
        return data;
    }


    /**
     * 查询二维码
     * @param businessTradeNo 生成二维码返回的流水号
     * @return
     * @throws OpenSDKException
     */
    public QueryQrRes query(String businessTradeNo) throws OpenSDKException{
        GenerateQrReq req = new GenerateQrReq();
        req.setBusinessId(accountInfo.getBusinessId());
        req.setBusinessTradeNo(businessTradeNo);
        String jsonRes = null;
        logger.info("query|req:{}",req);
        try {
            jsonRes = OkHttpUtil.postJson(QUERY_QR_URL,req);
        } catch (Exception e) {
            logger.error("query|error|",e);
            return null;
        }
        logger.info("query|res:{}",jsonRes);
        Type type = new ParameterizeTypeImpl(BaseRes.class, new Class[]{QueryQrRes.class});
        Gson gson = new Gson();
        BaseRes<QueryQrRes> res = gson.fromJson(jsonRes,type);
        if(0 != res.getCode()){
            logger.error("query|query qr code error|{}",res);
            throw new OpenSDKException(res.getCode(),res.getMsg());
        }
        QueryQrRes data = res.getData();
        logger.info("query|res:{}",data);
        return data;
    }


}
